<?php 
/**
 * 静态页面管理类
 * @author 王志昂
 * @create 2013-06-21 01:31:17
 */
class Business_Page
{
    protected $_pageTable;
    
    protected static $_instance;
    
    public static function getInstance()
    {
        if (empty(self::$_instance)) {
            self::$_instance = new Business_Page();
        }
        return self::$_instance;
    }
    
    public function __construct()
    {
        $this->_pageTable = Zend_Registry::get('database')->page;
    }
    
    /**
     * 获取所有的页面信息
     * @return array
     */
    public function getAllPageInfoById($typeid, $language)
    {
        $result = Utility_Db::getInstance()
            ->conn()
            ->select()
            ->from($this->_pageTable->tablename)
            ->where($this->_pageTable->typeid . '=?', $typeid)
            ->where($this->_pageTable->language . '=?', $language)
            ->query()
            ->fetch();
        
        return $result;
    }
    
    /**
     * 保存页面的内容
     * @param array $info
     * @return array
     */
    public function savePage($info)
    {
        try {
            $where = array(
                $this->_pageTable->id . '=?' => $info[$this->_pageTable->id]
            );
            $updateResult = Utility_Db::getInstance()
                ->conn()
                ->update($this->_pageTable->tablename, $info, $where);
            if ($updateResult) {
                $rtn = array(
                    'errorcode' => 0,
                    'errormsg' => 'update success'
                );
            } else {
                try {
                    $insertResult = Utility_Db::getInstance()
                        ->conn()
                        ->insert($this->_pageTable->tablename, $info);
                    if ($insertResult) {
                        $rtn = array(
                            'errorcode' => 0,
                            'errormsg' => 'insert success'
                        );
                    } else {
                        $rtn = array(
                            'errorcode' => -1,
                            'errormsg' => 'insert failed'
                        );
                    }
                } catch (Exception $e) {
                    $rtn = array(
                        'errorcode' => $e->getCode(),
                        'errormsg' => $e->getMessage()
                    );
                }
            }
        } catch (Exception $e) {
            $rtn = array(
                'errorcode' => $e->getCode(),
                'errormsg' => $e->getMessage()
            );
        }
        return $rtn;
    }
}